package com.mooc.mymooc.controller;

import com.mooc.mymooc.pojo.*;
import com.mooc.mymooc.service.MoocService;
import com.mooc.mymooc.utils.PageModel;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class OrgController {
    @Autowired
    private MoocService moocService;

    @RequestMapping(value = "/org_list")
    public String orgList(Model model,String keywords,Integer city,String ct,String sort,Integer pageIndex){
        Organization organization = new Organization();
        City city1 = new City();
        city1.setId(city);
        organization.setCity(city1);
        organization.setCt(ct);

        PageModel pageModel = new PageModel();
        if (pageIndex!=null) {
            pageModel.setPageIndex(pageIndex);
        }
        List<Organization> organizationList = this.moocService.findAllOrg(organization,sort,pageModel);
        List<City> cityList = this.moocService.findAllCity();
        model.addAttribute("org_list",organizationList);
        model.addAttribute("city_list",cityList);
        model.addAttribute("pageModel",pageModel);
        Integer counts = this.moocService.count();
        model.addAttribute("counts",organizationList.size());
        model.addAttribute("counts2",counts);
        List<Organization> top4 = this.moocService.findTop4Org();
        model.addAttribute("top4",top4);
        if (city!=null){
            model.addAttribute("city",city);
        }
        if (ct!=null){
            model.addAttribute("ct",ct);
        }
        if (sort!=null){
            model.addAttribute("sort",sort);
        }
        return "org-list";
    }

    @RequestMapping(value = "/org/add_ask")
    @ResponseBody
    public UserAsk addAsk(@RequestBody UserAsk userAsk){
        Integer rows = this.moocService.insertUserAsk(userAsk);
        if (rows>0){
            return new UserAsk();
        }else{
            return null;
        }
    }

    @RequestMapping(value = "/org_home")
    public String orgHome(Integer id,Model model,HttpServletRequest request){
        Organization organization = this.moocService.findOrgById(id);
        this.moocService.addClickNums_org(id);
        UserFavorite userFavorite = new UserFavorite();
        userFavorite.setFav_id(id);
        userFavorite.setFav_type(1);
        UserFavorite userFavorite1 = this.moocService.findIsFavIndex(userFavorite);
        User user = (User) request.getSession().getAttribute("user_session");
        model.addAttribute("org",organization);
        request.getSession().setAttribute("org",organization);
        if (userFavorite1!=null && user!=null){
            model.addAttribute("favflag","yes");
        }else{
            model.addAttribute("favflag","no");
        }
        return "org-detail-homepage";
    }

    @RequestMapping(value = "/org/add_fav")
    @ResponseBody
    public Object addFav(@RequestBody UserFavorite userFavorite,Model model,
                         HttpServletRequest request, HttpServletResponse response)throws Exception {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user_session");
        if (user==null){
            return null;
        }
        userFavorite.setUser(user);
        PrintWriter out = response.getWriter();
        Integer userFavId = this.moocService.findUserFav(userFavorite);
        if (userFavId != null) {
            this.moocService.removeUserFav(userFavId);
            if (userFavorite.getFav_type()==1){
                this.moocService.removeFav_nums_org(userFavorite.getFav_id());
            }else{
                if (userFavorite.getFav_type()==3){
                    this.moocService.removeFav_nums(userFavorite.getFav_id());
                }else{
                    if (userFavorite.getFav_type()==2){
                        this.moocService.updateFavnumsreduce(userFavorite.getFav_id());
                    }
                }
            }
        } else {
            Integer rowss = this.moocService.insertUserFav(userFavorite);
            if (userFavorite.getFav_type()==1){
                this.moocService.addFav_nums_org(userFavorite.getFav_id());
            }else{
                if (userFavorite.getFav_type()==3){
                    this.moocService.addFav_nums(userFavorite.getFav_id());
                }else{
                    if (userFavorite.getFav_type()==2){
                        this.moocService.updateFavnumsadd(userFavorite.getFav_id());
                    }
                }
            }
            if (rowss > 0) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("sta","ok");
                out.print(jsonObject.toString());
                return null;
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("sta","no");
        out.print(jsonObject.toString());
        return null;
        }

    @RequestMapping(value = "/org-detail-course")
    public String orgDetailCourse(HttpSession session, Integer pageIndex,
                                  Integer id, Model model){
        PageModel pageModel = new PageModel();
        Organization organization = this.moocService.findOrgById(id);
        int courseCount  = organization.getCourses().size();
        if (courseCount>0){
            pageModel.setRecordCount(courseCount);
        }
        if (pageIndex!=null){
            pageModel.setPageIndex(pageIndex);
        }
        pageModel.setRecordCount(organization.getCourses().size());
        Map map = new HashMap();
        map.put("org_id",organization.getId());
        map.put("pageModel",pageModel);
        List<Course> courseList = this.moocService.findByPage(map);
        model.addAttribute("org",organization);
        model.addAttribute("pageModel",pageModel);
        model.addAttribute("courses",courseList);
        UserFavorite userFavorite = new UserFavorite();
        userFavorite.setFav_id(id);
        userFavorite.setFav_type(1);
        UserFavorite userFavorite1 = this.moocService.findIsFavIndex(userFavorite);
        if (userFavorite1!=null && session.getAttribute("user_session")!=null){
            model.addAttribute("favflag","yes");
        }else{
            model.addAttribute("favflag","no");
        }

        return "org-detail-course";
    }

    @RequestMapping(value = "/org-detail-desc")
    public String orgDetailDesc(Model model,HttpSession session,Integer id){
        Organization organization = this.moocService.findOrgById(id);
        model.addAttribute("org",organization);
        UserFavorite userFavorite = new UserFavorite();
        userFavorite.setFav_id(id);
        userFavorite.setFav_type(1);
        UserFavorite userFavorite1 = this.moocService.findIsFavIndex(userFavorite);
        if (userFavorite1!=null && session.getAttribute("user_session")!=null){
            model.addAttribute("favflag","yes");
        }else{
            model.addAttribute("favflag","no");
        }
        return "org-detail-desc";
    }

    @RequestMapping(value = "/org-detail-teachers")
    public String orgDetailTeacher(HttpSession session,Integer id,Model model){
        Organization organization = this.moocService.findOrgById(id);
        UserFavorite userFavorite = new UserFavorite();
        userFavorite.setFav_id(id);
        userFavorite.setFav_type(1);
        UserFavorite userFavorite1 = this.moocService.findIsFavIndex(userFavorite);
        if (userFavorite1!=null && session.getAttribute("user_session")!=null){
            model.addAttribute("favflag","yes");
        }else{
            model.addAttribute("favflag","no");
        }
        model.addAttribute("org",organization);
        return "org-detail-teachers";
    }
}
